﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace PlateNumber {
    public class Option {

        public class OptionGeneral {
            public string Domain { get; set; }
            public string Home { get; set; }
        }

        public class OptionNetwork {
            public NetworkConnectionType ConnectionType { get; set; }
            //public bool UsingStaticProxyAtFirst { get; set; }
            public int AvailableProxyServerCountLowLimit { get; set; }
            public int StaticProxyServerResponseTime { get; set; }
            public int InvalidProxyServerResponseTime { get; set; }
        }

        public class OptionTask {
            public int Step0Delay { get; set; }
            public int Step1Delay { get; set; }
            public int Step2Delay { get; set; }
            public int Step3Delay { get; set; }
            public int Step4Delay { get; set; }
            public IfFoundUnknownPage IfFoundUnknownPage { get; set; }
            public IfNumberNotExist IfNumberNotExist { get; set; }
            public IfNumberHasBeUsed IfNumberHasBeUsed { get; set; }
            public int IPSwitchPeriod { get; set; }
            public bool CirculateTaskList { get; set; }
            public int IfFoundUnknownPageRetryTimes { get; set; }
            public int IfNumberNotExistRetryTimes { get; set; }
            public int IfNumberHasBeUsedRetryTimes { get; set; }
        }

        public enum IfFoundUnknownPage {
            Pause,
            Stop,
            Retry,
            Next
        }

        public enum IfNumberNotExist {
            QuickRetry,
            Retry,
            Next
        }

        public enum IfNumberHasBeUsed {
            QuickRetry,
            Retry,
            Next
        }

        public OptionGeneral General { get; set; }
        public OptionNetwork Network { get; set; }
        public OptionTask Task { get; set; }

        public static Option Load() {
            Option option = null;

            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Options", Program.ConnectionString);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dt.PrimaryKey = new[] { dt.Columns["OptionName"] };

            option = new Option();
            option.General = new OptionGeneral() {
                Domain = dt.Rows.Find("General_Domain")["OptionValue"].ToString(),
                Home = dt.Rows.Find("General_Home")["OptionValue"].ToString(),
            };
            option.Network = new OptionNetwork() {
                ConnectionType = (NetworkConnectionType)Int32.Parse(dt.Rows.Find("Network_ConnectionType")["OptionValue"].ToString()),
                //UsingStaticProxyAtFirst = Boolean.Parse(dt.Rows.Find("Network_UsingStaticProxyAtFirst")["OptionValue"].ToString()),
                AvailableProxyServerCountLowLimit = Int32.Parse(dt.Rows.Find("Network_AvailableProxyServerCountLowLimit")["OptionValue"].ToString()),
                StaticProxyServerResponseTime = Int32.Parse(dt.Rows.Find("Network_StaticProxyServerResponseTime")["OptionValue"].ToString()),
                InvalidProxyServerResponseTime = Int32.Parse(dt.Rows.Find("Network_InvalidProxyServerResponseTime")["OptionValue"].ToString())
            };
            option.Task = new OptionTask() {
                Step0Delay = Int32.Parse(dt.Rows.Find("Task_Step0Delay")["OptionValue"].ToString()),
                Step1Delay = Int32.Parse(dt.Rows.Find("Task_Step1Delay")["OptionValue"].ToString()),
                Step2Delay = Int32.Parse(dt.Rows.Find("Task_Step2Delay")["OptionValue"].ToString()),
                Step3Delay = Int32.Parse(dt.Rows.Find("Task_Step3Delay")["OptionValue"].ToString()),
                Step4Delay = Int32.Parse(dt.Rows.Find("Task_Step4Delay")["OptionValue"].ToString()),
                IfFoundUnknownPage = (IfFoundUnknownPage)Int32.Parse(dt.Rows.Find("Task_IfFoundUnknownPage")["OptionValue"].ToString()),
                IfNumberNotExist = (IfNumberNotExist)Int32.Parse(dt.Rows.Find("Task_IfNumberNotExist")["OptionValue"].ToString()),
                IfNumberHasBeUsed = (IfNumberHasBeUsed)Int32.Parse(dt.Rows.Find("Task_IfNumberHasBeUsed")["OptionValue"].ToString()),
                IPSwitchPeriod = Int32.Parse(dt.Rows.Find("Task_IPSwitchPeriod")["OptionValue"].ToString()),
                CirculateTaskList = Boolean.Parse(dt.Rows.Find("Task_CirculateTaskList")["OptionValue"].ToString()),
                IfFoundUnknownPageRetryTimes = Int32.Parse(dt.Rows.Find("Task_IfFoundUnknownPageRetryTimes")["OptionValue"].ToString()),
                IfNumberNotExistRetryTimes = Int32.Parse(dt.Rows.Find("Task_IfNumberNotExistRetryTimes")["OptionValue"].ToString()),
                IfNumberHasBeUsedRetryTimes = Int32.Parse(dt.Rows.Find("Task_IfNumberHasBeUsedRetryTimes")["OptionValue"].ToString())
            };

            return option;
        }

        public static void Save(Option option) {
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Options", Program.ConnectionString);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dt.PrimaryKey = new[] { dt.Columns["OptionName"] };

            dt.Rows.Find("General_Domain")["OptionValue"] = option.General.Domain;
            dt.Rows.Find("General_Home")["OptionValue"] = option.General.Home;

            dt.Rows.Find("Network_ConnectionType")["OptionValue"] = (int)option.Network.ConnectionType;
            //dt.Rows.Find("Network_UsingStaticProxyAtFirst")["OptionValue"] = option.Network.UsingStaticProxyAtFirst;
            dt.Rows.Find("Network_AvailableProxyServerCountLowLimit")["OptionValue"] = option.Network.AvailableProxyServerCountLowLimit;
            dt.Rows.Find("Network_StaticProxyServerResponseTime")["OptionValue"] = option.Network.StaticProxyServerResponseTime;
            dt.Rows.Find("Network_InvalidProxyServerResponseTime")["OptionValue"] = option.Network.InvalidProxyServerResponseTime;

            dt.Rows.Find("Task_Step0Delay")["OptionValue"] = option.Task.Step0Delay;
            dt.Rows.Find("Task_Step1Delay")["OptionValue"] = option.Task.Step1Delay;
            dt.Rows.Find("Task_Step2Delay")["OptionValue"] = option.Task.Step2Delay;
            dt.Rows.Find("Task_Step3Delay")["OptionValue"] = option.Task.Step3Delay;
            dt.Rows.Find("Task_Step4Delay")["OptionValue"] = option.Task.Step4Delay;
            dt.Rows.Find("Task_IfFoundUnknownPage")["OptionValue"] = (int)option.Task.IfFoundUnknownPage;
            dt.Rows.Find("Task_IfNumberNotExist")["OptionValue"] = (int)option.Task.IfNumberNotExist;
            dt.Rows.Find("Task_IfNumberHasBeUsed")["OptionValue"] = (int)option.Task.IfNumberHasBeUsed;
            dt.Rows.Find("Task_IPSwitchPeriod")["OptionValue"] = option.Task.IPSwitchPeriod;
            dt.Rows.Find("Task_CirculateTaskList")["OptionValue"] = option.Task.CirculateTaskList;
            dt.Rows.Find("Task_IfFoundUnknownPageRetryTimes")["OptionValue"] = option.Task.IfFoundUnknownPageRetryTimes;
            dt.Rows.Find("Task_IfNumberNotExistRetryTimes")["OptionValue"] = option.Task.IfNumberNotExistRetryTimes;
            dt.Rows.Find("Task_IfNumberHasBeUsedRetryTimes")["OptionValue"] = option.Task.IfNumberHasBeUsedRetryTimes;

            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
            da.Update(dt);
        }
    }


    public enum NetworkConnectionType {
        Local,
        Proxy,
        Ppp
    }
}
